<template>
  <div>
    <div style="width: 500px; margin: 0 auto;">
      <!-- 标题文字 -->
      <div><h1 style="color: #fff; text-align: center;">学茶商城运营管理平台</h1></div>
      <!-- 表单 -->
      <div style="background: #fff; padding: 50px 50px 20px 50px; border-radius: 8px;">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
          <el-form-item label="" prop="username">
            <el-input v-model="ruleForm.username">
              <el-button slot="prepend" icon="el-icon-user-solid"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input v-model="ruleForm.password">
              <el-button slot="prepend" icon="el-icon-s-ticket"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100%"
                type="primary" @click="submitForm('ruleForm')">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 版权信息 -->
      <div><p style="color: #fff; font-size: 12px; text-align: center;">Copyright &copy; 达内时代科技集团 - IT学院 - Java教学研发部</p></div>
    </div>
  </div>
</template>

<style>
body {
  height: 100vh;
  background-image: linear-gradient(to bottom left, #5abae8, #3c4bd2);
}
</style>

<script>
import BaseUrl from "@/http/request/BaseUrl";
import SimpleAxios from "@/http/request/SimpleAxios";

export default {
  data() {
    return {
      ruleForm: {
        username: 'root',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 15, message: '用户名必须由4~15个字符组成', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 32, message: '密码必须由4~32个字符组成', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = BaseUrl.PASSPORT + '/users/login';
          console.log('url = ' + url);

          SimpleAxios.post(url, this.ruleForm).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
              let loginInfo = jsonResult.data;
              localStorage.setItem('loginInfo', JSON.stringify(loginInfo));
              this.$router.push('/');
            } else {
              this.$alert(jsonResult.message, '提示', {
                confirmButtonText: '确定',
                callback: action => {}
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>